package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.ScoreTemplateConstant;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.ScoreLogDao;
import com.qdlc.p2p.dal.dto.ScoreLog;
import com.qdlc.p2p.dal.model.ScoreLogModel;

@Repository("scoreLogDao")
public class ScoreLogDaoImpl extends BaseDaoImpl<ScoreLog> implements ScoreLogDao {
	
	@Override
	public PageDataList<ScoreLogModel> findPageList(ScoreLogModel model) {
		QueryParam param = QueryParam.getInstance();
		if (StringUtil.isNotBlank(model.getSearchName())) {// 模糊查询
			param.addParam("user.userName", Operators.LIKE, model.getSearchName().trim());
		} else {// //精确查询
			if (model.getUser() != null && model.getUser().getUserId() > 0) {
				param.addParam("user.userId", model.getUser().getUserId());
			}
			if (StringUtil.isNotBlank(model.getUserName())) {
				param.addParam("user.userName", Operators.LIKE, model.getUserName().trim());
			}
			if (StringUtil.isNotBlank(model.getScoreTypeName())) {
				param.addParam("scoreTypeName", Operators.LIKE, model.getScoreTypeName());
			}
			if (model.getBehavior() != 99) {
				param.addParam("behavior", model.getBehavior());
			}
			if (StringUtil.isNotBlank(model.getStartTime())) {
				Date start = DateUtil.valueOf(model.getStartTime() + " 00:00:00");
				param.addParam("addTime", Operators.GTE, start);
			}
			if (StringUtil.isNotBlank(model.getEndTime())) {
				Date end = DateUtil.valueOf(model.getEndTime() + " 23:59:59");
				param.addParam("addTime", Operators.LTE, end);
			}
		}
		param.addParam("score", Operators.GT, 0);
		
		param.addOrder(OrderType.DESC, "id");
		param.addPage(model.getPage(), model.getRows());
		PageDataList<ScoreLog> itemPage = super.findPageList(param);
		List<ScoreLogModel> modelList = new ArrayList<ScoreLogModel>();
		PageDataList<ScoreLogModel> modelPage = new PageDataList<ScoreLogModel>();
		if (itemPage != null && itemPage.getList() != null && itemPage.getList().size() > 0) {
			for (int i = 0; i < itemPage.getList().size(); i++) {
				ScoreLog item = itemPage.getList().get(i);
				ScoreLogModel model_ = ScoreLogModel.instance(item);
				model_.setUserName(item.getUser().getUserName());
				modelList.add(model_);
			}
		}
		modelPage.setPage(itemPage.getPage());
		modelPage.setList(modelList);
		return modelPage;
	}

	@Override
	public ScoreLog findDaily(long userId) {
		String now = DateUtil.dateStr2(new Date());// 获取日期
		QueryParam param = QueryParam.getInstance();
		param.addParam("user.userId", userId);
		param.addParam("type",ScoreTemplateConstant.SCORE_USER_LOGIN);
		param.addParam("addTime", Operators.GTE, DateUtil.valueOf(now));
		return this.findByCriteriaForUnique(param);
	}

	@Override
	public ScoreLog findBeforeLog(long userId, String type) {
		String yesterday = DateUtil.dateStr2(DateUtil.rollDay(new Date(), -1)); // 获取前一日日期
		QueryParam param = QueryParam.getInstance();
		param.addParam("user.userId", userId);
		param.addParam("type", type);
		param.addParam("addTime", Operators.GTE, DateUtil.valueOf(yesterday));
		param.addParam("addTime", Operators.LTE, DateUtil.valueOf(yesterday + " 23:59:59"));
		return this.findByCriteriaForUnique(param);
	}

}
